package com.shop.milk_tea_shop;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

/**
 * @author 俞静雯
 * @Description 代码生成器
 * @date 2024-06-16  15:52
 */
public class CodeGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/a_milk_tea_shop?serverTimezon=UTC",
                        "root", "root123456")
                .globalConfig(builder -> {
                    builder.author("yjw") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(System.getProperty("user.dir") + "/src/main/java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.shop.milk_tea_shop") // 设置父包名
                                .pathInfo(Collections.singletonMap(OutputFile.xml,
                                        System.getProperty("user.dir") + "/src/main/java/com/shop/milk_tea_shop/mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude(new ArrayList<>(Arrays.asList("employee_info_table","order_detail_table",
                                        "point_detail_table", "user_info_table","item_category_table","item_info_table",
                                        "shop_info_table","item_add_category_table","item_add_table")))
                                .addTableSuffix("_table")
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
